package pub.wkq.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

//@Configuration  //方式一，方式二是在配置文件中配置
public class CorsConfig {
    @Bean
    public CorsWebFilter corsWebFilter() {
        //1. 添加 cors 配置相关信息
        CorsConfiguration config = new CorsConfiguration();
        //1.1 允许所有域名进行跨域调用
        config.addAllowedOriginPattern("*");
        //1.2 设置是否发送 cookie 信息
        config.setAllowCredentials(true);
        //1.3 设置允许请求的方式
        config.addAllowedMethod("*");
        //1.4 设置允许的header
        config.addAllowedHeader("*");
        //2. 为 url 添加映射
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        //3. 返回重新定义好的资源
        return new CorsWebFilter(source);
    }
}
